package com.asd.common.util;

// An highlighted block


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.javalite.http.Get;
import org.javalite.http.Http;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JwdUtil {


    /**
     * 根据地址返回经纬度---119.439735,32.484554
     * @param address
     * @return
     */
    public static String getLonLat(String address) {

        try {
            Get get = Http.get("http://restapi.amap.com/v3/geocode/geo?key=7a45f766e17439e931281e2c60618202&address=" + URLEncoder.encode(address, "utf-8"));
            String info = get.text();
//            log.info(info);
            if (StringUtils.isNotBlank(info)) {
                JSONObject json = JSONObject.parseObject(info);
                List<Map<String, Object>> addressList = (List<Map<String, Object>>) json.get("geocodes");
//                System.out.println(addressList.get(0).get("location"));
                if (!addressList.isEmpty()) {
                    return addressList.get(0).get("location").toString();
                }

            }
        } catch (Exception e) {
//            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 根据地址返回经纬度
     * {xcoor=119.439735, ycoor=32.484554}
     * @param address
     * @return
     */
    public static Map<String,String> getLonLat1(String address) {

        Map<String,String> map1 =new HashMap<String, String>();
        Map<String,String> map2 =new HashMap<String, String>();
        try {
            Get get = Http.get("http://restapi.amap.com/v3/geocode/geo?key=e3e2e32eca808770ed58ec70def2b932&address=" + URLEncoder.encode(address, "utf-8"));
            String info = get.text();
//            log.info(info);
            if (StringUtils.isNotBlank(info)) {
                JSONObject json = JSONObject.parseObject(info);
                List<Map<String, Object>> addressList = (List<Map<String, Object>>) json.get("geocodes");
//                System.out.println(addressList.get(0).get("location"));
                if (!addressList.isEmpty()) {
                    String str = addressList.get(0).get("location").toString();
                    String[] location = str.split(",");
                    String xcoor = location[0];
                    String ycoor = location[1];

                    map1.put("xcoor",xcoor);
                    map1.put("ycoor",ycoor);
                    return map1;
                }else {
                    String xccor ="";
                    String yccor ="";
                    map2.put("",xccor);
                    map2.put("",yccor);
                    return map2;
                }

            }
        } catch (Exception e) {
//            log.error(e.getMessage());
        }
        return null;
    }




    /**
     * 根据地址返回经纬度
     * {xcoor=119.439735, ycoor=32.484554}
     * @param address
     * @return
     */
    public static String[] getLonLat2(String address) {

        Map<String,String> map1 =new HashMap<String, String>();
        try {
            Get get = Http.get("http://restapi.amap.com/v3/geocode/geo?key=7a45f766e17439e931281e2c60618202&address=" + URLEncoder.encode(address, "utf-8"));
            String info = get.text();
//            log.info(info);
            if (StringUtils.isNotBlank(info)) {
                JSONObject json = JSONObject.parseObject(info);
                List<Map<String, Object>> addressList = (List<Map<String, Object>>) json.get("geocodes");
//                System.out.println(addressList.get(0).get("location"));
                if (!addressList.isEmpty()) {
                    String str = addressList.get(0).get("location").toString();
                    String[] location = str.split(",");

                    return location;
                }

            }else {

            }

        } catch (Exception e) {
//            log.error(e.getMessage());
        }
        return null;
    }




    public static void main(String[] args) {
        JwdUtil zhjwdUtil =new JwdUtil();
        try {
//            System.out.println(zhjwdUtil.getLonLat1("宝应县西安丰镇"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
